#pragma once

#include "Thread.h"
#include "TimeValue.h" // for default param
#include "RoomMessage.h"
#include "SimpleEvent.h"
#include "SimpleCriticalSection.h"
#include "RoomTimer.h"

namespace RoomFW {

struct IActiveObject;

//
// Class:	RoomTimerThread
//
// Description:	Thread to run extra room timers.  Windows only allows 64 objects per wait.
//
// Author:	Andrew Sage
//
class RoomTimerThread;
typedef ::boost::shared_ptr<RoomTimerThread> RoomTimerThreadSP;
class RoomTimerThread : public Thread
{
public:

	//
	// Method:	RoomTimerThread
	//
	// Description:	Constructor
	//
	// Parameters:
	//
	//	idMgr - Shared timer id manager.
	//
	RoomTimerThread(RoomTimer::IdMgr* idMgr);

	//
	// Method:	~RoomTimerThread
	//
	// Description:	Destructor
	//
	virtual ~RoomTimerThread();

	//
	// Method:	setTimer
	//
	// Description:	Set a timer.  When the timer expires, handler->put(expirationMessage)
	//
	// Parameters:
	//
	//	handler - Active object that will recieve the expiration message.
	//
	//	expirationMessage - Message generated by expiration.
	//
	//	expireIn - Time until first expiration.  See class notes for resolution.
	//
	//	repeatEvery - Interval to continue expiring.  See class notes for resolution.
	//					If 0, the timer does not repeat.
	//
	// Return:	Timer Id.  Needed to cancel the timer.
	//
	RoomTimer::TimerId setTimer(IActiveObject* handler,
								RoomMessageSP expirationMessage,
								const TimeValue& expireIn,
								const TimeValue& repeatEvery = 0);

	//
	// Method:	cancelTimer
	//
	// Description:	Cancel a timer.
	//
	// Parameters:
	//
	//	timerId - Timer id returned by setTimer().
	//
	// Return:	Success.
	//
	bool cancelTimer(RoomTimer::TimerId timerId);

	//
	// Thread override.  See Thread for documentation.
	//
	virtual const char* getThreadName() const;

protected:

	//
	// Thread overrides.  See Thread for documentation.
	//
	virtual bool initializeObject();
	virtual void main();
	virtual void signalStop();

private:

	//
	// Decouple implementation from base class (maybe just a little).
	//
	typedef Thread SUPER;

	//
	// Stop thread event.
	//
	SimpleEvent stopEvent_;

	//
	// Room Timer
	//
	RoomTimer timer_;

	//
	// Number of events this object directly owns.
	//
	static const size_t EventCount = 1;

	//
	// Not designed to be copied.
	//
	RoomTimerThread(const RoomTimerThread&);
	RoomTimerThread& operator =(const RoomTimerThread&);
};

}
